package com.metacube.questionbank.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import com.metacube.questionbank.model.Answer;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.model.User;
import com.metacube.questionbank.service.impl.AnswerServiceImpl;
import com.metacube.questionbank.service.impl.QuestionServiceImpl;
import com.metacube.questionbank.service.impl.TagServiceImpl;
import com.metacube.questionbank.service.impl.UserServiceImpl;
import com.metacube.questionbank.service.impl.VoteServiceImpl;

/*
 * @author Team Neeraj Jain
 * */

/*
 * This controller handles request that comes for the answers page.
 * */
@Controller
public class QuestionAnswerController {
	@Autowired
	AnswerServiceImpl answerService;
	@Autowired
	QuestionServiceImpl questionService;
	@Autowired
	VoteServiceImpl voteService;
	@Autowired
	UserServiceImpl userService;
	@Autowired
	TagServiceImpl tagService;

	/*
	 * @param req This method returns the view of answers page.The view contains
	 * the answers for a particular question.Each answer is associated with
	 * other information like who posted the answer,the no. of likes and
	 * dislikes.
	 * 
	 * @return modelAndView view of answers page.
	 */
	@RequestMapping(value = "QuestionAnswer.htm", method = RequestMethod.GET)
	public ModelAndView showQuestionAnswer(HttpServletRequest req) {
		int qId = Integer.parseInt(req.getParameter("id"));
		List<Answer> questionAnswers = answerService.getAnswers(qId);
		Question question = questionService.getQuestionByQId(qId);
		List<Integer> upVotes = voteService.getUpVotes(questionAnswers);
		List<Integer> downVotes = voteService
				.getDownVotes(questionAnswers);
		Integer answers = answerService.getAnswerCount(question);
		List<Tag> tag = tagService.getQuestionTags(question.getQuesId());
		List<User> users = userService.getUserByAId(questionAnswers);
		User qUser = userService.getUserQId(question);
		ModelAndView modelAndView = new ModelAndView("answers");
		req.setAttribute("answers", questionAnswers);
		req.setAttribute("question", question);
		req.setAttribute("upvotes", upVotes);
		req.setAttribute("downVotes", downVotes);
		req.setAttribute("ansCount", answers);
		req.setAttribute("tags", tag);
		req.setAttribute("user", users);
		req.setAttribute("qUser", qUser);
		return modelAndView;
	}

	/*
	 * @param req This method is same as above.The only difference is that it
	 * called when the user posts a answer.It shows the updated answers for that
	 * question.
	 * 
	 * @return modelAndView view of answers page.
	 */
	@RequestMapping(value = "QuestionAnswer.htm", method = RequestMethod.POST)
	public ModelAndView postAnswer(HttpServletRequest req, HttpSession session) {
		User objUser = (User) session.getAttribute("user");
		String answer = req.getParameter("description");
		Answer objAnswer = new Answer();
		objAnswer.setAnswerDesc(answer);
		int qId = Integer.parseInt(req.getParameter("id"));
		objAnswer.setQuestionId(qId);
		objAnswer.setUserId(objUser.getUser_id());
		answerService.insertAnswer(objAnswer);
		ModelAndView modelAndView = showQuestionAnswer(req);
		return modelAndView;
	}

	/*
	 * @param answerId id of answer that is liked or disliked.
	 * 
	 * @param userId id of user who liked or disliked the answer.
	 * 
	 * @param upvoteStatus contains 1 or -1(1 for like and -1 for dislike).This
	 * method handles ajax request when the user likes or dislikes a answer.
	 * 
	 * @return votes the updated likes and dislikes for that answer.
	 */
	@RequestMapping(value = "p.json", method = RequestMethod.GET)
	public @ResponseBody List<Integer> upvote(
			@RequestParam(value = "answerId") int answerId,
			@RequestParam(value = "userId") int userId,
			@RequestParam(value = "upvoteStatus") int upvoteStatus) {
		voteService.insertUpvoteStatus(userId, answerId, upvoteStatus);
		int upVotes = voteService.getUpvotesForAnswer(answerId);
		int downVotes = voteService.getDownvotesForAnswer(answerId);
		List<Integer> votes = new ArrayList<Integer>();
		votes.add(upVotes);
		votes.add(downVotes);
		return votes;
	}
}